package com.duapk.index.conf;

//import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

/**
 * 配置接口对象
 * 
 * @Package: com.duapk.index.conf
 * @ClassName: ConfigFactory
 * @author: jiangzheng
 * @date: Apr 5, 2011 4:54:05 PM
 * 
 */
public class ConfigFactory {

	private static final String CONFIG_FILE_DEFAULT_PATH = "/opt/search/index/conf";
	private static XMLConfiguration config = null;

	// 方便在系统启动时候
	public static void init(String configFilePath) {
		// 如果未配置获取默认值
		if (configFilePath == null) {
			configFilePath = CONFIG_FILE_DEFAULT_PATH;
		}
		try {
			config = new XMLConfiguration(configFilePath);
			config.setReloadingStrategy(new FileChangedReloadingStrategy());
		} catch (ConfigurationException e) {
			System.out.println("Fatal:Create Config Object Error!!!");
			System.exit(1);
		}
	}

	// 不允许外部实例化
	private ConfigFactory() {
	}

	/**
	 * 获取配置的字符串值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @return
	 */
	public static String getString(String configXPath) {
		return config.getString(configXPath, null);
	}

	/**
	 * 获取配置的字符串值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @param defaultValue
	 *            配置项的默认值
	 * @return
	 */
	public static String getString(String configXPath, String defaultValue) {
		return config.getString(configXPath, defaultValue);
	}

	/**
	 * 获取配置的整数值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @return
	 */
	public static int getInt(String configXPath) {
		return config.getInt(configXPath);
	}

	/**
	 * 获取配置的整数值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @param defaultValue
	 *            配置项的默认值
	 * @return
	 */
	public static int getInt(String configXPath, int defaultValue) {
		return config.getInt(configXPath, defaultValue);
	}

	/**
	 * 获取配置的float
	 * 
	 * @param configXPath
	 * @return
	 */
	public static float getFloat(String configXPath) {
		return config.getFloat(configXPath, 1.0f);
	}

	/**
	 * 获取配置的boolean值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @return
	 */
	public static boolean getBoolean(String configXPath) {
		return config.getBoolean(configXPath);
	}

	/**
	 * 获取配置的boolean值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @param defaultValue
	 *            配置项的默认值
	 * @return
	 */
	public static boolean getBoolean(String configXPath, boolean defaultValue) {
		return config.getBoolean(configXPath, defaultValue);
	}

	/**
	 * 获取配置的List值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getList(String configXPath) {
		return config.getList(configXPath);
	}

	/**
	 * 获取配置的List值
	 * 
	 * @param configXPath
	 *            配置项路径
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getList(String configXPath,
			List<String> defaultValue) {
		return config.getList(configXPath, defaultValue);
	}

	public static void main(String[] args) {
		System.out.println(ConfigFactory.getString("module.param1"));
	}

}
